package au.edu.uq.nmerge;
/*
 *  NMerge is Copyright 2009 Desmond Schmidt
 * 
 *  This file is part of NMerge. NMerge is a Java library for merging 
 *  multiple versions into multi-version documents (MVDs), and for 
 *  reading, searching and comparing them.
 *
 *  NMerge is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  NMerge is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.util.HashMap;
import au.edu.uq.nmerge.mvd.Match;
import au.edu.uq.nmerge.mvd.MVD;

/**
 * Cache search results for later calls to get the next match
 * @author Desmond Schmidt 22/9/07
 */
public class Cache extends HashMap<String,MVD>
{
	/** required for serializable classes */
	public static final long serialVersionUID = 1;
	/** the current index into the cached matches */
	int matchIndex;
	/** true if the last search was a multi-version one */
	boolean multiVersion;
	/** the matches generated by the last search */
	Match[] matches;
	/** getNextMatch checks that the new pattern equals this old one */
	byte[] pattern;
	/** database connection properties file */
	String dbConn;
	public Cache( String dbConn )
	{
		super();
		this.dbConn = dbConn;
	}
	/**
	 * Get the database connection properties file path
	 * @return a String
	 */
	public String getDbConn()
	{
		return this.dbConn;
	}
	/**
	 * Get the next match if available
	 * @param pattern the pattern which must match the cached one
	 * @return an array containing a single Match or an empty array
	 */
	Match[] getNextMatch( byte[] pattern )
	{
		Match[] matched = new Match[1];
		// last pattern may have found nothing
		if ( patternSame(pattern) && matches != null 
			&& matches.length > 0 )
		{
			if (  matchIndex == matches.length )
				matchIndex = 0;
			matched[0] = matches[matchIndex++];
			return matched;
		}
		else
			return new Match[0];
	}
	/**
	 * Clear the match array
	 */
	void clearMatches()
	{
		matches = null;
	}
	/**
	 * Find out if the last search was multi-version
	 * @return true if it was
	 */
	boolean getMultiVersion()
	{
		return multiVersion;
	}
	/**
	 * Set the multi-version search flag
	 * @param multiVersion true if multi-version search is desired 
	 */
	void setMultiVersion( boolean multiVersion )
	{
		this.multiVersion = multiVersion;
	}
	/**
	 * After completing a fresh search, store the matches here for 
	 * later retrieval.
	 * @param matches a new set of matches to cache
	 * @param pattern the pattern for which the matches were made
	 */
	void saveMatches( Match[] matches, byte[] pattern )
	{
		this.matches = matches;
		this.pattern = pattern;
		matchIndex = 0;
	}
	/**
	 * Does the new pattern equal the cached one? In this case we will 
	 * perform a find-next.
	 * @param pattern the new pattern
	 * @return true if the new pattern equals the old pattern
	 */
	private boolean patternSame( byte[] pattern )
	{
		if ( this.pattern != null && this.pattern.length == pattern.length )
		{
			for ( int i=0;i<pattern.length;i++ )
				if ( pattern[i] != this.pattern[i] )
					return false;
			return true;
		}
		else
			return false;
	}
}
